<template>
    <div class="tableContainer">
        <el-form :inline="true"  class="demo-form-inline" @submit.native.prevent>
            <el-form-item label="">
                <el-input class="elInput" v-model="name" placeholder="输入权限名称"></el-input>
            </el-form-item>
            
            <el-form-item>
                <el-button class="elBtn" type="primary" @click="selectAuth">查询</el-button>
                <el-button class="elBtn" type="primary" @click="openDialog('','',1)">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" max-height="88%" border style="width: 100%" >
            <el-table-column type="index" min-width="3%" align="center"></el-table-column>
            <el-table-column prop="name" label="权限名称" min-width="15%" align="center"> </el-table-column>
            <el-table-column prop="description" label="描述" min-width="35%" align="center"></el-table-column>
             <el-table-column prop="menuName" label="权限菜单" min-width="20%" align="center" :show-overflow-tooltip="true" ></el-table-column>
            <el-table-column prop="op" label="操作" align="center" min-width="20%">
                <template slot-scope="scope">
                    <el-button class="elBtn"  type="primary" size="mini" icon="edit"  @click="openDialog(scope.$index, scope.row,2)">修改</el-button>
                    <el-button size="mini" icon="delete" type="danger" @click="openDialog(scope.$index, scope.row,3)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background 
			layout="prev, pager, next, sizes, total, jumper"
			:page-sizes="pagesizes"
			:page-size="pagesize"
			:total="totalNum"
            @current-change="changePage"
            @size-change="sizeChange"
			>
		</el-pagination>

        <!-- 对话框 -->
        <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="50%" max-height="80%" center @close="clearForm">
            <el-form :model="form" ref="form">
                <el-form-item label="权限名称" :label-width="formLabelWidth" prop="name" :rules="{validator:this.$common.emptyValidate, trigger: 'blur'}">
                    <el-input class="elInput" v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" :label-width="formLabelWidth" prop="description">
                    <el-input class="elInput" type="textarea" v-model="form.description" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="权限菜单" :label-width="formLabelWidth" prop="menu_id">
                    <!-- <el-input class="elInput" v-model="form.menu_id" autocomplete="off"></el-input> -->
                    <el-tree
                        :data="menuList"
                        show-checkbox
                        default-expand-all
                        :check-strictly="true"
                        node-key="menuId"
                        ref="tree"
                        >
                    </el-tree>
                </el-form-item>
                <!-- <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="status" active-color="#13ce66" inactive-color="#ccc"  inactive-text="禁用" active-text="启用">
                    </el-switch>
                </el-form-item> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="clearForm">取 消</el-button>
                <el-button class="elBtn" type="primary" @click="saveAuth">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="提示" :visible.sync="centerDialogVisible" width="30%" center>
            <span>确定删除这条权限信息</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible = false">取 消</el-button>
                <el-button class="elBtn" type="primary" @click="doDelete">确 定</el-button>
            </span>
        </el-dialog>
    </div>
    

</template>

<script>
export default {
    data() {
        return {
            dialogFormVisible:false, // 控制新增/修改对话框的显示隐藏
            centerDialogVisible:false, // 控制删除对话框的显示隐藏
            authId:"", // 选择的角色id
            dialogTitle:"新增单位", // 对话框标题
            pagesizes:[1,15,20,25],
            pagesize:1,
            currpage: 1,
            status:true,
            totalNum:0,
            name: '', // 查询条件
            form: { // 新增/修改表单参数
                id:"",
                name: '',
                description:"",
                menuId:"01",
            },
            tableData: [],
            menuList:[],
            formLabelWidth: '20%', // label宽度
            tipMsg:"",
            clientHeight:"" // 屏幕高度
      }
    },
    created(){
        // 获取屏幕高度
        this.clientHeight = this.$common.getClientHeight()
        this.pagesize = Math.ceil((this.clientHeight - 250)/55)
        this.pagesizes[0] = this.pagesize

        this.selectAuth()
        // 获取所有菜单数据
        this.$common.selectAllMenu(res => {
            this.menuList = this.$common.dataFormat(res.data.data)
        })
    },
    methods:{
        
        // 打开对话框
        openDialog(index,row,type){
            if(type == 3){ // 删除
                this.centerDialogVisible = true
                this.authId = row.id
            }else if(type == 2){ // 修改
                
                this.authId = row.id
                this.selectById()
                this.dialogTitle = "修改权限信息"
            }
            else if(type == 1){ // 新增
                this.dialogFormVisible = true
                this.dialogTitle = "新增权限信息"
            }
            
        },
        // 查询权限
        selectAuth(){
            this.$api.selectAuth({params:{key:this.name,page:this.currpage,limit:this.pagesize}}).then(result=>{
                // filterLogin(result)
                var _this = this
                this.$common.filterLogin(result,_this,()=>{
                    if(result.data.success){
                        result.data.data.rows.forEach(element => {
                            element.menuName = []
                            if(element.menuList && element.menuList.length>0){
                                element.menuList.forEach(e => {
                                    element.menuName.push(e.name)
                                })
                                element.menuName = element.menuName.join(',')
                            }
                        })
                        _this.tableData = result.data.data.rows
                        _this.totalNum = result.data.data.totalRows
                    }
                })
            })
        },
        // 根据id查询
        selectById(){
            this.$api.selectAuthById({params:{
                id:this.authId
            }}).then(result=>{
                var _this = this
                this.$common.filterLogin(result,_this,()=>{
                    if(result.data.success){
                        this.dialogFormVisible = true
                    }
                    _this.form = result.data.data
                    // 设置菜单勾选
                    _this.$nextTick(() => {
                         _this.$refs.tree.setCheckedKeys(result.data.data.menuId.split(","),false)
                    });
                   
                })

                
            })
        },
        // 保存
        saveAuth(){
            this.$refs["form"].validate(valid=> {
                if(valid){
                    if(!this.form.id){ // 新增
                        this.form.menuId = this.$refs.tree.getCheckedKeys().join(",")
                        this.$api.addAuth(this.form).then(result=>{
                            this.successFun(result,1)
                            
                        })
                    }else{ // 修改
                        this.form.menuId = this.$refs.tree.getCheckedKeys().join(",")
                        this.$api.modifyAuth(this.form).then(result=>{
                            this.successFun(result,2)
                        })
                    }
                }
            })
            
            
        },
        // 删除权限信息
        doDelete(){
            this.$api.deleteAuth(this.authId).then(result=>{
                this.successFun(result,3)
            })
        },
        // 接口调用成功的方法
        successFun(result,type){
            var _this = this
            this.$common.filterLogin(result,_this,()=>{
                _this.clearForm(type)
                _this.selectAuth()  
                if(result.data.success){
                    _this.$message({center: true,message:result.data.message?result.data.message:"操作成功",type: 'success'});
                }else{
                    this.$message({center: true,message: result.data.message?result.data.message:"网络错误,请稍后再试",type: 'warning'});
                }
            })
        },
        // 清空表单
        clearForm(type){
            this.centerDialogVisible = false
            this.dialogFormVisible = false
            // 清楚表单数据
            for(let item in this.form){
                this.form[item] = ""
            }
            type !=3?this.$refs['form'].clearValidate():""
            type !=3?this.$refs.tree.setCheckedKeys([],false):""
        },
        // 分页--改变当前页
        changePage(index){
            this.currpage = index
            this.selectAuth()
        },
        // 分页--改变当前页显示条数
        sizeChange(page){
            this.pagesize = page
            this.selectAuth()
        }
        
    },
    filters:{
    }
}
</script>

<style lang="scss" scoped>
.tableContainer{
    height: 97%;
    padding: 10px;
    background: #FDFDFD;
    // background: transparent;
    border-radius: 5px;
    // box-shadow: 5px 5px 4px rgba(204, 204, 204, 0.5)
    .el-pagination.is-background{
        text-align: right;
        padding:10px 5px;
    }
    .el-table,.el-table th, .el-table tr{
        background: transparent !important;
    }
}
</style>